package org.easyupdater;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class ApplicationStartup {

	private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

	private Runnable taskToRun;

	private int executionInterval;

	private ScheduledFuture<?> scheduledFuture;

	public synchronized void init() {
		if (taskToRun != null) {
			scheduledFuture = scheduler.scheduleAtFixedRate(taskToRun, 0, executionInterval,
					TimeUnit.SECONDS);
		}
	}

	public synchronized void destroy() {
		if (scheduledFuture != null) {
			scheduledFuture.cancel(false);
			scheduler.shutdown();
		}
	}

	public Runnable getTaskToRun() {
		return taskToRun;
	}

	public void setTaskToRun(Runnable taskToRun) {
		this.taskToRun = taskToRun;
	}

	public int getExecutionInterval() {
		return executionInterval;
	}

	public void setExecutionInterval(int executionInterval) {
		this.executionInterval = executionInterval;
	}
}
